<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Utilities.UIFunctions"%>
<%@page import="Utilities.Constants.MiscellaneousRequestType"%>
<%@page import="Beans.Requests.DataRetrieval.MiscDataRetrievalRequest"%>
<%@page import="Beans.Responses.DataRetrievalComponents.MiscDataComponent"%>
<%@page import="Beans.Responses.DataRetrievalResponse"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Managers.Utilities.User"%>
<%@page import="org.virtualbox_3_2.MediumVariant"%>
<%@page import="org.virtualbox_3_2.StorageControllerType"%>
<%@page import="org.virtualbox_3_2.StorageBus"%>
<%@page import="org.virtualbox_3_2.NetworkAttachmentType"%>
<%@page import="org.virtualbox_3_2.NetworkAdapterType"%>
<%@page import="org.virtualbox_3_2.VRDPAuthType"%>
<%@page import="org.virtualbox_3_2.BIOSBootMenuMode"%>
<%@page import="org.virtualbox_3_2.FirmwareType"%>
<%@page import="org.virtualbox_3_2.AudioDriverType"%>
<%@page import="org.virtualbox_3_2.DeviceType"%>
<%@page import="org.virtualbox_3_2.AudioControllerType"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc"%>
<%@taglib uri="/WEB-INF/tlds/pagePrintingTags.tld" prefix="p"%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);

    //data retrieval requests
    MiscDataRetrievalRequest mediumFormatsRequest = null;
    MiscDataRetrievalRequest osTypesRequest = null;
    //data retrieval responses
    DataRetrievalResponse<MiscDataComponent> mediumFormatsData = null;
    DataRetrievalResponse<MiscDataComponent> osTypesData = null;

    //builds the medium formats retrieval request
    mediumFormatsRequest = new MiscDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                        userSessionData.getCurrentServerId(),
                                                        MiscellaneousRequestType.MEDIUM_FORMATS);

    //builds the guest OS types retrieval request
    osTypesRequest = new MiscDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                  userSessionData.getCurrentServerId(),
                                                  MiscellaneousRequestType.GUEST_OS_TYPES);

    //attempts to send the medium formats request and retrieves the response
    if(UIFunctions.sendServiceRequest(request, response, mediumFormatsRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
        mediumFormatsData = (DataRetrievalResponse<MiscDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
    else
        ;

    //attempts to send the guest OS types request and retrieves the response
    if(UIFunctions.sendServiceRequest(request, response, osTypesRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
        osTypesData = (DataRetrievalResponse<MiscDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
    else
        ;
%>
<p:printPageHeader pageName="Create Virtual Machine" pageId="pageMachines" userSessionData="<%= userSessionData %>"/>
    <form id="formCreateMachine" action="manage_machine.jsp?action=1" method="post">
        <table id="mainConfiguration" class="mainResourceDataTable">
            <tr><td>Machine Name:</td><td><input type="text" name="create.machine.name"></td></tr>
            <tr><td>Machine Description:</td><td><input type="text" name="create.machine.description"></td></tr>
            <tr><td>Operating System Type:</td>
                <td><select name="create.machine.osType"><option value="0" selected disabled>Select Guest OS Type</option>
                        <p:printOSTypes osTypesData="<%= osTypesData %>"/></select></td></tr>
            <tr><td>Machine Permissions:</td><td><input type="text" size="2" name="create.machine.permissions"></td></tr>
            <tr><td><input type="submit" value="Create Machine"></td><td></td></tr>
        </table>
        <table id="advanceConfiguration" class="mainResourceDataTable">
        <tr><td>Advanced Configuration</td></tr>
        <tr><td>Audio:</td><td>
        <input type="checkbox" name="create.machine.audio.configure" onchange="changeMachineCreationElementState('audioConfiguration', this);"></td><td></td>
        <td><table id="audioConfiguration" class="resourceCreationElement">
                <tr><td>Enabled:</td><td><input type="checkbox" name="create.machine.audio.enable"></td></tr>
                <tr><td>Controller Type:</td><td><select name="create.machine.audio.controllerType">
                                                    <option value="<%= AudioControllerType.AC_97 %>">AC'97</option>
                                                    <option value="<%= AudioControllerType.SB_16 %>">SoundBlaster 16</option>
                                                 </select></td></tr>
                <tr><td>Driver Type:</td><td><select name="create.machine.audio.driverType">
                                                <option value="<%= AudioDriverType.NULL %>">none</option>
                                                <option value="<%= AudioDriverType.ALSA %>">ALSA (L)</option>
                                                <option value="<%= AudioDriverType.CORE_AUDIO %>">CoreAudio (M)</option>
                                                <option value="<%= AudioDriverType.DIRECT_SOUND %>">DirectSound (W)</option>
                                                <option value="<%= AudioDriverType.MMPM %>">MMPM</option>
                                                <option value="<%= AudioDriverType.OSS %>">OSS (L)</option>
                                                <option value="<%= AudioDriverType.PULSE %>">PulseAudio (L)</option>
                                                <option value="<%= AudioDriverType.SOL_AUDIO %>">Solaris Audio (S)</option>
                                                <option value="<%= AudioDriverType.WIN_MM %>">Windows Multimedia (W)</option>
                                             </select></td></tr>
            </table></td></tr>

        <tr><td>BIOS:</td><td>
        <input type="checkbox" name="create.machine.bios.configure" onchange="changeMachineCreationElementState('biosConfiguration', this);"></td><td></td>
        <td><table id="biosConfiguration" class="resourceCreationElement">
                <tr><td>Firmware Type:</td><td><select name="create.machine.bios.firmware">
                                                 <option value="<%= FirmwareType.BIOS %>"><%= FirmwareType.BIOS %></option>
                                                 <option value="<%= FirmwareType.EFI %>"><%= FirmwareType.EFI %></option>
                                                 <option value="<%= FirmwareType.EFIDUAL %>"><%= FirmwareType.EFIDUAL %></option>
                                                 <option value="<%= FirmwareType.EFI_32 %>"><%= FirmwareType.EFI_32 %></option>
                                                 <option value="<%= FirmwareType.EFI_64 %>"><%= FirmwareType.EFI_64 %></option>
                                               </select></td></tr>
                <tr><td>HPET Enabled:</td><td><input type="checkbox" name="create.machine.bios.hpetEnabled"></td></tr>
                <tr><td>Run RTC in UTC:</td><td><input type="checkbox" name="create.machine.bios.RTCinUTC"></td></tr>
                <tr><td>Boot Menu Mode:</td><td><select name="create.machine.bios.bootMenuMode">
                                                    <option value="<%= BIOSBootMenuMode.DISABLED %>">Disabled</option>
                                                    <option value="<%= BIOSBootMenuMode.MENU_ONLY %>">Menu Only</option>
                                                    <option value="<%= BIOSBootMenuMode.MESSAGE_AND_MENU %>">Message and Menu</option>
                                                 </select></td></tr>
                <tr><td>ACPI Enabled:</td><td><input type="checkbox" name="create.machine.bios.acpiEnabled"></td></tr>
                <tr><td>IOAPIC Enabled:</td><td><input type="checkbox" name="create.machine.bios.ioapicEnabled"></td></tr>
                <tr><td>Host Time Offset:</td><td><input type="text" size="2" name="create.machine.bios.timeOffset"> ms</td></tr>
            </table></td></tr>

        <tr><td>Boot:</td><td>
        <input type="checkbox" name="create.machine.boot.configure" onchange="changeMachineCreationElementState('bootConfiguration', this);"></td><td></td>
        <td><table id="bootConfiguration" class="resourceCreationElement">
                <jstlc:forEach varStatus="loop" begin="0" end="3">
                    <tr><td>Slot #${loop.index}</td><td><select name="create.machine.boot.slot${loop.index}">
                          <option value="<%= DeviceType.NULL %>">none</option>
                          <option value="<%= DeviceType.DVD %>">DVD</option>
                          <option value="<%= DeviceType.FLOPPY %>">Floppy</option>
                          <option value="<%= DeviceType.HARD_DISK %>">Hard Disk</option>
                          <option value="<%= DeviceType.NETWORK %>">Network</option>
                          <option value="<%= DeviceType.SHARED_FOLDER %>">Shared Folder</option>
                          <option value="<%= DeviceType.USB %>">USB</option>
                        </select></td></tr>
                </jstlc:forEach>
            </table></td></tr>

        <tr><td>CPU:</td><td>
        <input type="checkbox" name="create.machine.cpu.configure" onchange="changeMachineCreationElementState('cpuConfiguration', this);"></td><td></td>
        <td><table id="cpuConfiguration" class="resourceCreationElement">
                <tr><td>Hot Plugging Enabled:</td><td><input type="checkbox" name="create.machine.cpu.hotPluggingEnabled"></td></tr>
                <tr><td>Available CPUs:</td><td><input type="text" size="2" name="create.machine.cpu.count"></td></tr>
            </table></td></tr>

        <tr><td>I/O Cache:</td><td>
        <input type="checkbox" name="create.machine.io.configure" onchange="changeMachineCreationElementState('ioConfiguration', this);"></td><td></td>
        <td><table id="ioConfiguration" class="resourceCreationElement">
                <tr><td>Cache Enabled:</td><td><input type="checkbox" name="create.machine.io.cacheEnabled"></td></tr>
                <tr><td>Cache Size:</td><td><input type="text" size="3" name="create.machine.io.cacheSize"> MB</td></tr>
                <tr><td>Cache Max Bandwidth:</td><td><input type="text" size="3" name="create.machine.io.cacheMaxBandwidth"> MB/s</td></tr>
            </table></td></tr>

        <tr><td>Memory:</td><td>
        <input type="checkbox" name="create.machine.memory.configure" onchange="changeMachineCreationElementState('memoryConfiguration', this);"></td><td></td>
        <td><table id="memoryConfiguration" class="resourceCreationElement">
                <tr><td>RAM Size:</td><td><input type="text" size="3" name="create.machine.memory.ramSize"> MB</td></tr>
                <tr><td>RAM Balloon Size:</td><td><input type="text" size="3" name="create.machine.memory.ramBalloonSize"> MB</td></tr>
                <tr><td>Page Fusion Enabled:</td><td><input type="checkbox" name="create.machine.memory.pageFusionEnabled"></td></tr>
            </table></td></tr>

        <tr><td>Network Adapters:</td><td>
        <input type="checkbox" name="create.machine.networks.configure" onchange="changeMachineCreationElementState('networkAdaptersConfiguration', this);"></td><td></td>
        <td><table id="networkAdaptersConfiguration" class="resourceCreationElement">
                <tr><td><input type="hidden" name="create.machine.networks.numberOfEnabledAdapters" value="0"></td></tr>
                <tr id="networkAdaptersAdditionLinkRow"><td><a class="actionLink" href="noJavaScript.jsp" onclick="return addNetworkAdapterForm('networkAdaptersConfiguration');">Add&nbsp;Adapter</a></td></tr>
                <tr><td><table id="networkAdapterTemplate" class="templateElement">
                    <tr><td><input type="hidden" name=""></td></tr>
                    <tr><td>Enabled:</td><td><input type="checkbox" name=""></td></tr>
                    <tr><td>Adapter Type:</td><td><select name="">
                                                    <option value="<%= NetworkAdapterType.NULL %>">none</option>
                                                    <option value="<%= NetworkAdapterType.AM_79_C_970_A %>">PCnet-PCI II (Am79C970A)</option>
                                                    <option value="<%= NetworkAdapterType.AM_79_C_973 %>">PCnet-FAST III (Am79C973)</option>
                                                    <option value="<%= NetworkAdapterType.I_82540_EM %>">Intel PRO/1000 MT Desktop (82540EM)</option>
                                                    <option value="<%= NetworkAdapterType.I_82543_GC %>">Intel PRO/1000 T Server (82543GC)</option>
                                                    <option value="<%= NetworkAdapterType.I_82545_EM %>">Intel PRO/1000 MT Server (82545EM)</option>
                                                    <option value="<%= NetworkAdapterType.VIRTIO %>">Paravirtualized Network (virtio-net)</option>
                                                  </select></td></tr>
                    <tr><td>Network Type:</td><td><select name="">
                                                    <option value="<%= NetworkAttachmentType.NULL %>">none</option>
                                                    <option value="<%= NetworkAttachmentType.BRIDGED %>">Bridged</option>
                                                    <option value="<%= NetworkAttachmentType.HOST_ONLY %>">Host-Only</option>
                                                    <option value="<%= NetworkAttachmentType.INTERNAL %>">Internal</option>
                                                    <option value="<%= NetworkAttachmentType.NAT %>">NAT</option>
                                                    <option value="<%= NetworkAttachmentType.VDE %>">VDE</option>
                                                  </select></td></tr>
                    <tr><td>MAC Address:</td><td><input type="text" size="12" name=""></td></tr>
                    <tr><td>Network Name:</td><td><input type="text" size="36" name=""><input type="hidden" name="" disabled></td></tr>
                    <tr><td>Line Speed:</td><td><input type="text" size="6" name=""></td></tr>
                    <tr><td>Cable Connected:</td><td><input type="checkbox" name=""></td></tr>
                    <tr><td></td></tr></table></td></tr>
            </table></td></tr>

        <tr><td>Shared Folders:</td><td>
        <input type="checkbox" name="create.machine.folders.configure" onchange="changeMachineCreationElementState('sharedFoldersConfiguration', this);"></td><td></td>
        <td><table id="sharedFoldersConfiguration" class="resourceCreationElement">
                <tr><td><input type="hidden" id="create.machine.folders.newNumber" name="create.machine.folders.newNumber" value="0"></td></tr>
                <tr><td>Folder&nbsp;Name</td><td>Host&nbsp;Path</td><td>Writable</td><td></td></tr>
                <tr id="sharedFolderTemplateRow" class="templateElement"><td><input type="text" name=""></td><td><input type="text" name=""></td><td><input type="checkbox" name=""></td><td></td></tr>
                <tr><td><a class="actionLink" href="noJavaScript.jsp" onclick="return addSharedFolderForm('sharedFoldersConfiguration', 'create.machine.folders')">Add&nbsp;Folder</a></td></tr></table></td></tr>

        <tr><td>Storage:</td><td>
        <input type="checkbox" name="create.machine.storage.configure" onchange="changeMachineCreationElementState('storageConfiguration', this);"></td><td></td>
            <td><div id="storageConfiguration" class="resourceCreationElement">
                Controllers Settings: <a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('controllersConfigurationTable', true);">View</a><br>
                <input type="hidden" id="create.machine.controllers.newNumber" name="create.machine.controllers.newNumber" value="0">
                <table id="controllersConfigurationTable" class="resourceCreationElement">
                    <tr><td>Controller&nbsp;Name</td><td>Connection&nbsp;Type</td><td>Controller&nbsp;Type</td><td>Host&nbsp;I/O&nbsp;Cache</td></tr>
                    <tr id="controllerTemplateRow" class="templateElement"><td><input type="text" name=""></td><td><select name="">
                                <option value="<%= StorageBus.NULL %>"><%= StorageBus.NULL %></option>
                                <option value="<%= StorageBus.FLOPPY %>"><%= StorageBus.FLOPPY %></option>
                                <option value="<%= StorageBus.IDE %>"><%= StorageBus.IDE %></option>
                                <option value="<%= StorageBus.SAS %>"><%= StorageBus.SAS %></option>
                                <option value="<%= StorageBus.SATA %>"><%= StorageBus.SATA %></option>
                                <option value="<%= StorageBus.SCSI %>"><%= StorageBus.SCSI %></option>
                            </select></td><td><select name="">
                                <option value="<%= StorageControllerType.NULL %>">none</option>
                                <option value="<%= StorageControllerType.BUS_LOGIC %>">BusLogic</option>
                                <option value="<%= StorageControllerType.ICH_6 %>">ICH6</option>
                                <option value="<%= StorageControllerType.INTEL_AHCI %>">Intel AHCI</option>
                                <option value="<%= StorageControllerType.I_82078 %>">I82078</option>
                                <option value="<%= StorageControllerType.LSI_LOGIC %>">LsiLogic</option>
                                <option value="<%= StorageControllerType.LSI_LOGIC_SAS %>">LsiLogic SAS</option>
                                <option value="<%= StorageControllerType.PIIX_3 %>">PIIX3</option>
                                <option value="<%= StorageControllerType.PIIX_4 %>">PIIX4</option>
                            </select></td><td><input type="checkbox" name=""></td><td></td></tr>
                    <tr><td><a class="actionLink" href="noJavaScript.jsp" onclick="return addControllerForm('controllersConfigurationTable', 'create.machine.controllers');">Add Controller</a></td></tr></table>
                Attachments Settings: <a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('attachmentsConfigurationTable', true);">View</a><br>
                <input type="hidden" id="create.machine.attachments.newNumber" name="create.machine.attachments.newNumber" value="0">
                <table id="attachmentsConfigurationTable" class="resourceCreationElement">
                    <tr><td>Medium&nbsp;ID</td><td>Controller</td><td>Controller&nbsp;Port</td><td>Controller&nbsp;Slot</td>
                        <td>Device&nbsp;Type</td></tr>
                    <tr id="attachmentTemplateRow" class="templateElement"><td><input type="text" size="36" name=""></td><td><input name=""></td><td><input size="2" name=""></td><td><input size="2" name=""></td><td><select name="">
                                        <option value="<%= DeviceType.NULL %>">none</option>
                                        <option value="<%= DeviceType.HARD_DISK %>">Hard Disk</option>
                                        <option value="<%= DeviceType.DVD %>">DVD</option>
                                        <option value="<%= DeviceType.FLOPPY %>">Floppy</option>
                                    </select></td><td></td><td></td><td></td></tr>
                    <tr><td><a class="actionLink" href="noJavaScript.jsp" onclick="return addAttachmentForm('attachmentsConfigurationTable', 'create.machine.attachments', true);">Add New Attachment</a></td></tr></table>
                <table id="newMediumAttachmentElement" class="templateElement">
                    <tr><td><input type="hidden" name="" value="1"></td></tr>
                    <tr><td>Medium Format:</td><td><select name=""><option value="0" selected disabled>Select</option>
                    <jstlc:choose>
                        <jstlc:when test="<%= mediumFormatsData != null && mediumFormatsData.getRequestResult() %>">
                        <jstlc:forEach var="mediumFormat" items="<%= mediumFormatsData.getResource().getMediumFormats() %>">
                            <option value="${mediumFormat.formatId}">${mediumFormat.formatName}</option>
                        </jstlc:forEach>
                        </jstlc:when>
                        <jstlc:otherwise>
                        <option value="0">N/A</option>
                        </jstlc:otherwise>
                    </jstlc:choose>
                    </select></td></tr>
                    <tr><td>Medium Location:</td><td><input type="text" name=""></td></tr>
                    <tr><td>Medium Variant:</td><td><select name="">
                        <option value="<%= MediumVariant.STANDARD %>">Standard</option>
                        <option value="<%= MediumVariant.DIFF %>">Differencing</option>
                        <option value="<%= MediumVariant.FIXED %>">Fixed</option>
                        <option value="<%= MediumVariant.VMDK_ESX %>"><%= MediumVariant.VMDK_ESX %></option>
                        <option value="<%= MediumVariant.VMDK_SPLIT_2_G %>"><%= MediumVariant.VMDK_SPLIT_2_G %></option>
                        <option value="<%= MediumVariant.VMDK_STREAM_OPTIMIZED %>"><%= MediumVariant.VMDK_STREAM_OPTIMIZED %></option>
                    </select></td></tr>
                    <tr><td>Permissions:</td><td><input type="text" size="2" name=""></td></tr>
                    <tr><td>Medium Size:</td><td><input type="text" size="3" name=""> MB</td></tr>
                    <tr><td>Parent UUID:</td><td><input type="text" size="36" name="" disabled></td></tr>
                </table>
            </div></td></tr>

        <tr><td>VRDP Server:</td><td>
        <input type="checkbox" name="create.machine.vrdp.configure" onchange="changeMachineCreationElementState('vrdpConfiguration', this);"></td><td></td>
        <td><table id="vrdpConfiguration" class="resourceCreationElement">
                <tr><td>Server Enabled:</td><td><input type="checkbox" name="create.machine.vrdp.serverEnabled"></td></tr>
                <tr><td>Listening Port(s):</td><td><input type="text" value="Automatically Generated" disabled></td></tr>
                <tr><td>Listening Address:</td><td><input type="text" name="create.machine.vrdp.serverAddress"></td></tr>
                <tr><td>Authentication Type:</td><td><select name="create.machine.vrdp.authType">
                                                       <option value="<%= VRDPAuthType.NULL %>">none</option>
                                                       <option value="<%= VRDPAuthType.EXTERNAL %>">External</option>
                                                       <option value="<%= VRDPAuthType.GUEST %>">Guest</option>
                                                     </select></td></tr>
                <tr><td>Authentication Timeout:</td><td><input type="text" size="3" name="create.machine.vrdp.timeout"> ms</td></tr>
                <tr><td>Multiple Connections Allowed:</td><td><input type="checkbox" name="create.machine.vrdp.allowMultipleConnections"></td></tr>
                <tr><td>Reuse Single Connection:</td><td><input type="checkbox" name="create.machine.vrdp.reuseSingleConnection"></td></tr>
                <tr><td>RDP Video Channel State:</td><td><input type="checkbox" name="create.machine.vrdp.videoChannelEnabled"></td></tr>
                <tr><td>RDP Video Channel Quality:</td><td><input type="text" size="2" name="create.machine.vrdp.videoChannelQuality"> %</td></tr>
            </table></td></tr>

        <tr><td>Video:</td><td>
        <input type="checkbox" name="create.machine.video.configure" onchange="changeMachineCreationElementState('videoConfiguration', this);"></td><td></td>
        <td><table id="videoConfiguration" class="resourceCreationElement">
                <tr><td>VRAM Size:</td><td><input type="text" size="2" name="create.machine.video.vramSize"> MB</td></tr>
                <tr><td>3D Acceleration Enabled:</td><td><input type="checkbox" name="create.machine.video.3DEnabled"></td></tr>
                <tr><td>2D Acceleration Enabled:</td><td><input type="checkbox" name="create.machine.video.2DEnabled"></td></tr>
            </table></td></tr>
        </table>
    </form>
<p:printPageFooter/>